package com.mingda.weathermagiccenter;

import java.awt.Color;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TT {
    public static List<String> generateColorGradient(String[] colors, int targetCount) {
        List<String> result = new ArrayList<>();

        if (colors.length < 2) {
            throw new IllegalArgumentException("At least two colors are required to generate a gradient.");
        }

        if (targetCount < 1) {
            throw new IllegalArgumentException("Target count must be a positive integer.");
        }

        int segmentCount = colors.length - 1;
        int colorsPerSegment = targetCount / segmentCount;
        int remainingColors = targetCount % segmentCount;

        for (int i = 0; i < segmentCount; i++) {
            int[] startRgb = hexToRgb(colors[i]);
            int[] endRgb = hexToRgb(colors[i + 1]);

            // Calculate the step size for each color channel
            double rStep = (endRgb[0] - startRgb[0]) / ((double) colorsPerSegment);
            double gStep = (endRgb[1] - startRgb[1]) / ((double) colorsPerSegment);
            double bStep = (endRgb[2] - startRgb[2]) / ((double) colorsPerSegment);

            // Generate the colors for the current segment
            for (int j = 0; j < colorsPerSegment; j++) {
                int r = (int) Math.round(startRgb[0] + j * rStep);
                int g = (int) Math.round(startRgb[1] + j * gStep);
                int b = (int) Math.round(startRgb[2] + j * bStep);
                result.add(rgbToHex(r, g, b));
            }

            // Add the remaining colors to the last segment
            if (i == segmentCount - 1 && remainingColors > 0) {
                for (int j = 0; j < remainingColors; j++) {
                    int r = (int) Math.round(startRgb[0] + (colorsPerSegment + j) * rStep);
                    int g = (int) Math.round(startRgb[1] + (colorsPerSegment + j) * gStep);
                    int b = (int) Math.round(startRgb[2] + (colorsPerSegment + j) * bStep);
                    result.add(rgbToHex(r, g, b));
                }
            }
        }

        // Add the last color
        result.add(colors[colors.length - 1]);

        return result;
    }

    private static int[] hexToRgb(String hex) {
        if (hex.startsWith("#")) {
            hex = hex.substring(1);
        }

        int r = Integer.parseInt(hex.substring(0, 2), 16);
        int g = Integer.parseInt(hex.substring(2, 4), 16);
        int b = Integer.parseInt(hex.substring(4, 6), 16);

        return new int[]{r, g, b};
    }

    private static String rgbToHex(int r, int g, int b) {
        return String.format("#%02X%02X%02X", r, g, b);
    }


    //      color: '#5466EC'
    //    - value: 0
    //      color: '#6E8FDE'
    //    - value: 0
    //      color: '#82BAC9'
    //    - value: 0
    //      color: '#9EE1BE'
    //    - value: 0
    //      color: '#C4F490'
    //    - value: 0
    //      color: '#E7E35B'
    //    - value: 0
    //      color: '#FDC238'
    //    - value: 0
    //      color: '#FE9D36'
    //    - value: 0
    //      color: '#FF7739'
    //    - value: 0
    //      color: '#FE5337'
    //    - value: 50
    //      color: '#FE5337'
    public static void main(String[] args) {
      //  String[] colors = {"#5466EC","#6E8FDE","#82BAC9","#9EE1BE","#C4F490","#E7E35B","#FDC238","#FE9D36","#FF7739","#FE5337"};
/*        String[] colors = {"#000000","#01213d","#01284b","#013059","#013767","#013e74","#014582","#014d90","#01549e","#005bac","#0063ba","#006ac8","#0071d5","#0078e3","#0080f1","#0087ff","#0087ff","#0c90ff","#1898ff","#24a1ff","#31a9ff","#3db2ff","#49baff","#55c3ff","#61ccff","#6dd4ff","#79ddff","#86e5ff","#92eeff","#9ef6ff","#aaffff","#aaffff","#befcfc","#d2fafa","#e6f7f7","#e3e6d3","#d8e6a6","#cce678","#c1e64a","#b5e61d","#b5e61d","#c8ec40","#daf364","#edf987","#ffffaa","#ffffaa","#fff197","#ffe384","#ffd471","#ffc65e","#ffb84c","#ffaa39","#ff9b26","#ff8d13","#ff7f00","#ff7f00","#ff7201","#ff6502","#ff5803","#ff4b04","#ff3e06","#ff3107","#ff2408","#ff1709","#ff0a0a","#ff0a0a","#df0808","#bf0505" };
        System.out.println(colors.length);
        int targetCount = 71;
        int i = -35;
        List<String> gradient = generateColorGradient(colors, targetCount);
        for (String color : gradient) {
            System.out.println("    - value: "+ (i) );
            System.out.println("      color: '"+color+"'");
            i += 1;
        }
    }*/


        String[] colors = {"#000000","#01213d","#01284b","#013059","#013767","#013e74","#014582","#014d90","#01549e","#005bac","#0063ba","#006ac8","#0071d5","#0078e3","#0080f1","#0087ff","#0087ff","#0c90ff","#1898ff","#24a1ff","#31a9ff","#3db2ff","#49baff","#55c3ff","#61ccff","#6dd4ff","#79ddff","#86e5ff","#92eeff","#9ef6ff","#aaffff","#aaffff","#befcfc","#d2fafa","#e6f7f7","#e3e6d3","#d8e6a6","#cce678","#c1e64a","#b5e61d","#b5e61d","#c8ec40","#daf364","#edf987","#ffffaa","#ffffaa","#fff197","#ffe384","#ffd471","#ffc65e","#ffb84c","#ffaa39","#ff9b26","#ff8d13","#ff7f00","#ff7f00","#ff7201","#ff6502","#ff5803","#ff4b04","#ff3e06","#ff3107","#ff2408","#ff1709","#ff0a0a","#ff0a0a","#df0808","#bf0505" };
        System.out.println(colors.length);
        int targetCount = 71;
        int i = -35;
        List<String> gradient = Arrays.asList(colors);
        for (String color : gradient) {
            System.out.println("    - value: "+ (i) );
            System.out.println("      color: '"+color+"'");
            i += 1;
        }
    }
}  